package com.daisuxia.back.service;

import static com.daisuxia.web.pojo.BorrowOrder.STATUS_BFHK;
import static com.daisuxia.web.pojo.BorrowOrder.STATUS_HKZ;
import static com.daisuxia.web.pojo.BorrowOrder.STATUS_YHZ;
import static com.daisuxia.web.pojo.BorrowOrder.STATUS_YYQ;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang3.StringUtils;
import org.jsoup.helper.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.daisuxia.back.utils.ServiceResult;
import com.daisuxia.risk.service.IOutOrdersService;
import com.daisuxia.web.dao.IBorrowOrderDao;
import com.daisuxia.web.dao.IBorrowOrderLoanDao;
import com.daisuxia.web.dao.IBorrowOrderLoanPersonDao;
import com.daisuxia.web.dao.IRepaymentDao;
import com.daisuxia.web.dao.IUserDao;
import com.daisuxia.web.pojo.BorrowOrder;
import com.daisuxia.web.pojo.BorrowOrderLoan;
import com.daisuxia.web.pojo.BorrowOrderLoanPerson;
import com.daisuxia.web.pojo.OutOrders;
import com.daisuxia.web.pojo.Repayment;
import com.daisuxia.web.service.IBorrowOrderService;
import com.daisuxia.web.service.IChannelReportService;
import com.daisuxia.web.service.IRepaymentService;
import com.daisuxia.web.test.HttpRequestCMBDF;
import com.daisuxia.web.test.HttpRequestCMBHL;
import com.daisuxia.web.test.XmlPacket;
import com.daisuxia.web.util.DateUtil;
import com.daisuxia.web.util.GenerateNo;

@Component("taskJob")
public class TaskJob implements ITaskJob {
	Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private IRepaymentService repaymentService;
	@Autowired
	private IOutOrdersService outOrdersService;
	@Autowired
	private IRepaymentDao repaymentDao;
	@Autowired
	private IBorrowOrderDao borrowOrderDao;
	@Autowired
	private IUserDao userDao;
	@Autowired
	private IBorrowOrderService borrowOrderService;
	@Autowired
	private IBorrowOrderLoanDao borrowOrderLoanDao;
	@Autowired
	private IBorrowOrderLoanPersonDao borrowOrderLoanPersonDao;
	@Autowired
	private IChannelReportService channelReportService;

	@Override
	public void queryPaysStateNotCmb() {
		logger.info("queryPaysStateNotCmb  starting ");
		try {
			String orderNo = GenerateNo.nextOrdId();
			// 生成请求报文
			String requestParams = HttpRequestCMBHL.getRequestNTQRYEBPStr();

			// 插入订单
			OutOrders orders = new OutOrders();
			orders.setOrderNo(orderNo);
			orders.setOrderType(OutOrders.orderType_cmb);
			// orders.setAssetOrderId(borrowOrder.getId());
			orders.setReqParams(requestParams);
			orders.setAddTime(new Date());
			orders.setAct("NTQRYEBP");
			orders.setStatus(OutOrders.STATUS_WAIT);
			outOrdersService.insert(orders);
			// 连接前置机，发送请求报文，获得返回报文
			String result = HttpRequestCMBHL.sendRequest(requestParams);
			// 更新订单
			OutOrders ordersNew = new OutOrders();
			ordersNew.setId(orders.getId());
			ordersNew.setOrderNo(orderNo);
			ordersNew.setReturnParams(result);
			ordersNew.setUpdateTime(new Date());
			ordersNew.setStatus(OutOrders.STATUS_SUC);
			outOrdersService.update(ordersNew);

			if (result != null && result.length() > 0) {

				String jsonstring = HttpRequestCMBDF.xml2JSON(result);

				JSONObject jsonObj = JSONObject.fromObject(jsonstring);
				JSONArray arrays = jsonObj.getJSONArray("NTWAUEBPZ");
				if (arrays != null && arrays.size() > 0) {
					for (int i = 0; i < arrays.size(); i++) {
						JSONObject obj = arrays.getJSONObject(i);

						// Map propPayResult = (Map) ((Vector)
						// propPayResultMap.get("NTWAUEBPZ")).get(i);

						String sREQSTS = obj.getString("REQSTS");
						String sRTNFLG = obj.getString("RTNFLG");
						String sYURREF = obj.getString("YURREF");// 业务号

						if (StringUtils.isNotBlank(sYURREF)) {
//							if (sYURREF.equals("A1114830730868251")) {
//
//								System.out.println();
//							}
							ServiceResult serviceResult = getPayNotCmbresult(obj, sREQSTS, sRTNFLG);
							String code = serviceResult.getCode();
							String payRemark = serviceResult.getMsg();
							HashMap<String, Object> params = new HashMap<String, Object>();
							params.put("notPayStatus", BorrowOrder.SUB_PAY_SUCC);
							params.put("yurref", sYURREF);
							Date fkDate = new Date();
							if (sYURREF.startsWith("A")) {

								BorrowOrder borrowOrder = null;

								borrowOrder = borrowOrderDao.selectByParam(params);

								if (borrowOrder != null && !borrowOrder.getPaystatus().equals(BorrowOrder.SUB_PAY_SUCC) && borrowOrder.getStatus() == BorrowOrder.STATUS_FKZ) {

									BorrowOrder borrowOrderNew = new BorrowOrder();
									borrowOrderNew.setId(borrowOrder.getId());
									// borrowOrderNew.setPaystatus(paystatus);
									borrowOrderNew.setPayRemark(payRemark);
									borrowOrderNew.setUpdatedAt(fkDate);

									if (code.equals(BorrowOrder.SUB_PAY_SUCC)) {

										borrowOrderNew.setPaystatus(code);
										borrowOrderNew.setLoanTime(fkDate);
										borrowOrderNew.setStatus(BorrowOrder.STATUS_HKZ);

										borrowOrderNew.setLoanTime(fkDate);
										borrowOrderNew.setLoanEndTime(DateUtil.addDay(fkDate, borrowOrder.getLoanTerm()));// 放款时间加上借款期限

										// 放款成功插入还款记录
										borrowOrder.setPaystatus(code);
										borrowOrder.setStatus(BorrowOrder.STATUS_HKZ);
										borrowOrder.setLoanTime(fkDate);
										borrowOrder.setLoanEndTime(borrowOrderNew.getLoanEndTime());// 放款时间加上借款期限

										repaymentService.insertByBorrorOrder(borrowOrder);

										// 设置B可打款

										params.clear();
										params.put("assetOrderId", borrowOrder.getId());
										params.put("status", BorrowOrderLoan.CSZT);
										BorrowOrderLoan bdl = borrowOrderLoanDao.selectByParam(params);
										if (bdl != null) {
											BorrowOrderLoan bdlNew = new BorrowOrderLoan();
											bdlNew.setId(bdl.getId());
											bdlNew.setUpdatedAt(fkDate);
											bdlNew.setStatus(BorrowOrderLoan.DFK);
											borrowOrderLoanDao.updateByPrimaryKeySelective(bdlNew);
										}
										// 设置C可打款
										BorrowOrderLoanPerson bdlPerson = borrowOrderLoanPersonDao.selectByParam(params);
										BorrowOrderLoanPerson bdlPersonNew = new BorrowOrderLoanPerson();
										if (bdlPerson != null) {
											bdlPersonNew.setId(bdlPerson.getId());
											bdlPersonNew.setUpdatedAt(fkDate);
											bdlPersonNew.setStatus(BorrowOrderLoan.DFK);
											borrowOrderLoanPersonDao.updateByPrimaryKeySelective(bdlPersonNew);
										}
									} else if (code.equals(BorrowOrder.PAY_PAY_FAIL)) {

										borrowOrderNew.setPaystatus(code);
										borrowOrderNew.setStatus(BorrowOrder.STATUS_FKSB);
									}
									borrowOrderDao.updateByPrimaryKeySelective(borrowOrderNew);
								} else {
									logger.error("queryRequestPays error   找不到对应的借款记录或者该记录已经被处理过:业务号：" + sYURREF);
								}

							} else if (sYURREF.startsWith("B")) {

								List<BorrowOrderLoan> bdls = borrowOrderLoanDao.findParams(params);
								if (bdls != null && bdls.size() > 0) {
									BorrowOrderLoan bdlNew = new BorrowOrderLoan();
									bdlNew.setYurref(sYURREF);
									bdlNew.setPayRemark(payRemark);
									bdlNew.setPayStatus(code);
									bdlNew.setUpdatedAt(fkDate);
									if (code.equals(BorrowOrder.SUB_PAY_SUCC)) {
										bdlNew.setStatus(BorrowOrderLoan.FKCG);
										bdlNew.setLoanTime(fkDate);
									}
									borrowOrderLoanDao.updateByYurref(bdlNew);
								}
							} else if (sYURREF.startsWith("C")) {
								// 批量处理
								List<BorrowOrderLoanPerson> bdlPersons = borrowOrderLoanPersonDao.findParams(params);
								if (bdlPersons != null && bdlPersons.size() > 0) {
									BorrowOrderLoanPerson bdlPersonNew = new BorrowOrderLoanPerson();
									// bdlPersonNew.setId(bdlPerson.getId());
									bdlPersonNew.setYurref(sYURREF);
									bdlPersonNew.setPayRemark(payRemark);
									bdlPersonNew.setPayStatus(code);
									bdlPersonNew.setUpdatedAt(fkDate);
									if (code.equals(BorrowOrder.SUB_PAY_SUCC)) {
										bdlPersonNew.setStatus(BorrowOrderLoan.FKCG);
										bdlPersonNew.setLoanTime(fkDate);
									}
									borrowOrderLoanPersonDao.updateByYurref(bdlPersonNew);
								}
							}
						}

					}
				}

			}

		} catch (Exception e) {
			logger.error("queryPaysStateNotCmb   errror",e);
			e.printStackTrace();
		}
	}

	@Override
	public void queryPaysStateCmb() {
		logger.info("queryPaysStateCmb  starting ");
		try {
			String orderNo = GenerateNo.nextOrdId();
			// // 生成请求报文
			String requestParams = HttpRequestCMBDF.getRequestNTQRYEBPStr();

			// 插入订单
			OutOrders orders = new OutOrders();
			orders.setOrderNo(orderNo);
			orders.setOrderType(OutOrders.orderType_cmb);
			// orders.setAssetOrderId(borrowOrder.getId());
			orders.setReqParams(requestParams);
			orders.setAddTime(new Date());
			orders.setAct("NTQRYEBP");
			orders.setStatus(OutOrders.STATUS_WAIT);
			outOrdersService.insert(orders);
			// 连接前置机，发送请求报文，获得返回报文
			String result = HttpRequestCMBDF.sendRequest(requestParams);
			logger.info("resultParams queryPaysStateCmb:" + result);
			// 更新订单
			OutOrders ordersNew = new OutOrders();
			ordersNew.setId(orders.getId());
			ordersNew.setOrderNo(orderNo);
			ordersNew.setReturnParams(result);
			ordersNew.setUpdateTime(new Date());
			ordersNew.setStatus(OutOrders.STATUS_SUC);
			outOrdersService.update(ordersNew);

			String jsonstring = HttpRequestCMBDF.xml2JSON(result);

			JSONObject jsonObj = JSONObject.fromObject(jsonstring);
			// JSONArray arrays = jsonObj.getJSONArray("NTQATSQYZ");
			Object objj = jsonObj.get("NTQATSQYZ");
			JSONArray arrays = null;
			if (objj instanceof JSONObject) {
				arrays = new JSONArray();
				arrays.add(objj);

			} else if (objj instanceof JSONArray) {

				arrays = jsonObj.getJSONArray("NTQATSQYZ");
			}

			if (arrays != null && arrays.size() > 0) {
				for (int i = 0; i < arrays.size(); i++) {
					JSONObject obj = arrays.getJSONObject(i);

					String sYURREF = obj.getString("YURREF");
					String sREQSTA = obj.getString("REQSTA");
					String sRTNFLG = obj.getString("RTNFLG");

					if (StringUtils.isNotBlank(sYURREF)) {

						ServiceResult serviceResult = getPayCmbresult(obj, sREQSTA, sRTNFLG);// new
																								// ServiceResult(BorrowOrder.PAY_ERROR,
																								// "未知异常，请稍后重试！");

						String code = serviceResult.getCode();
						String payRemark = serviceResult.getMsg();
						HashMap<String, Object> params = new HashMap<String, Object>();
						params.put("notPayStatus", BorrowOrder.SUB_PAY_SUCC);
						params.put("yurref", sYURREF);
						Date fkDate = new Date();
						if (sYURREF.startsWith("A")) {

							BorrowOrder borrowOrder = null;

							borrowOrder = borrowOrderDao.selectByParam(params);

							if (borrowOrder != null && !borrowOrder.getPaystatus().equals(BorrowOrder.SUB_PAY_SUCC) && borrowOrder.getStatus() == BorrowOrder.STATUS_FKZ) {

								BorrowOrder borrowOrderNew = new BorrowOrder();
								borrowOrderNew.setId(borrowOrder.getId());
								// borrowOrderNew.setPaystatus(paystatus);
								borrowOrderNew.setPayRemark(payRemark);
								borrowOrderNew.setUpdatedAt(fkDate);

								if (code.equals(BorrowOrder.SUB_PAY_SUCC)) {

									borrowOrderNew.setPaystatus(code);
									borrowOrderNew.setLoanTime(fkDate);
									borrowOrderNew.setStatus(BorrowOrder.STATUS_HKZ);

									borrowOrderNew.setLoanTime(fkDate);
									borrowOrderNew.setLoanEndTime(DateUtil.addDay(fkDate, borrowOrder.getLoanTerm()));// 放款时间加上借款期限

									// 放款成功插入还款记录
									borrowOrder.setPaystatus(code);
									borrowOrder.setStatus(BorrowOrder.STATUS_HKZ);
									borrowOrder.setLoanTime(fkDate);
									borrowOrder.setLoanEndTime(borrowOrderNew.getLoanEndTime());// 放款时间加上借款期限

									repaymentService.insertByBorrorOrder(borrowOrder);

									// 设置B可打款

									params.clear();
									params.put("assetOrderId", borrowOrder.getId());
									params.put("status", BorrowOrderLoan.CSZT);
									BorrowOrderLoan bdl = borrowOrderLoanDao.selectByParam(params);
									if (bdl != null) {
										BorrowOrderLoan bdlNew = new BorrowOrderLoan();
										bdlNew.setId(bdl.getId());
										bdlNew.setUpdatedAt(fkDate);
										bdlNew.setStatus(BorrowOrderLoan.DFK);
										borrowOrderLoanDao.updateByPrimaryKeySelective(bdlNew);
									}
									// 设置C可打款
									BorrowOrderLoanPerson bdlPerson = borrowOrderLoanPersonDao.selectByParam(params);
									BorrowOrderLoanPerson bdlPersonNew = new BorrowOrderLoanPerson();
									if (bdlPerson != null) {
										bdlPersonNew.setId(bdlPerson.getId());
										bdlPersonNew.setUpdatedAt(fkDate);
										bdlPersonNew.setStatus(BorrowOrderLoan.DFK);
										borrowOrderLoanPersonDao.updateByPrimaryKeySelective(bdlPersonNew);
									}
								} else if (code.equals(BorrowOrder.PAY_PAY_FAIL)) {

									borrowOrderNew.setPaystatus(code);
									borrowOrderNew.setStatus(BorrowOrder.STATUS_FKSB);
								}
								borrowOrderDao.updateByPrimaryKeySelective(borrowOrderNew);
							} else {
								logger.error("queryRequestPays error   找不到对应的借款记录或者该记录已经被处理过:订单号：" + sYURREF);
							}

						} else if (sYURREF.startsWith("B")) {
							params.put("status", BorrowOrderLoan.DKZ);
							List<BorrowOrderLoan> bdls = borrowOrderLoanDao.findParams(params);
							if (bdls != null && bdls.size() > 0) {
								BorrowOrderLoan bdlNew = new BorrowOrderLoan();
								bdlNew.setYurref(sYURREF);
								bdlNew.setPayRemark(payRemark);
								bdlNew.setPayStatus(code);
								bdlNew.setUpdatedAt(fkDate);
								if (code.equals(BorrowOrder.SUB_PAY_SUCC)) {
									bdlNew.setStatus(BorrowOrderLoan.FKCG);
									bdlNew.setLoanTime(fkDate);
								}
								borrowOrderLoanDao.updateByYurref(bdlNew);
							}
						} else if (sYURREF.startsWith("C")) {
							params.put("status", BorrowOrderLoan.DKZ);
							// 批量处理
							List<BorrowOrderLoanPerson> bdlPersons = borrowOrderLoanPersonDao.findParams(params);
							if (bdlPersons != null && bdlPersons.size() > 0) {
								BorrowOrderLoanPerson bdlPersonNew = new BorrowOrderLoanPerson();
								// bdlPersonNew.setId(bdlPerson.getId());
								bdlPersonNew.setYurref(sYURREF);
								bdlPersonNew.setPayRemark(payRemark);
								bdlPersonNew.setPayStatus(code);
								bdlPersonNew.setUpdatedAt(fkDate);
								if (code.equals(BorrowOrder.SUB_PAY_SUCC)) {
									bdlPersonNew.setStatus(BorrowOrderLoan.FKCG);
									bdlPersonNew.setLoanTime(fkDate);
								}
								borrowOrderLoanPersonDao.updateByYurref(bdlPersonNew);
							}
						}
					}

				}
			}

		} catch (Exception e) {
			logger.error("queryPaysStateCmb   errror",e);
		 
		}
	}

	/**
	 * 计算支付结果 非招行
	 * 
	 * @param obj
	 * @param sREQSTS
	 * @param sRTNFLG
	 * @return
	 */
	private ServiceResult getPayNotCmbresult(JSONObject obj, String sREQSTS, String sRTNFLG) {

		ServiceResult serviceResult = new ServiceResult(BorrowOrder.PAY_ERROR, "未知异常，请稍后重试！");
		try {

			if (sREQSTS.equals("FIN") && sRTNFLG.equals("F")) {
				serviceResult = new ServiceResult(BorrowOrder.PAY_PAY_FAIL, "支付失败");
				logger.info(serviceResult.getCode() + ">>" + serviceResult.getMsg());
			} else if (sREQSTS.equals("FIN") && sRTNFLG.equals("S")) {

				serviceResult = new ServiceResult(BorrowOrder.SUB_PAY_SUCC, "支付成功");
				logger.info(serviceResult.getCode() + ">>" + serviceResult.getMsg());
			} else {
				serviceResult = new ServiceResult(BorrowOrder.PAY_SUBMIT, "支付已被银行受理（支付状态：" + sREQSTS + "）");
				logger.info(serviceResult.getCode() + ">>" + serviceResult.getMsg());

			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		return serviceResult;

	}

	/**
	 * 计算支付结果 招行
	 * 
	 * @param propPayResult
	 * @param sREQSTS
	 * @param sRTNFLG
	 * @return
	 */
	private ServiceResult getPayCmbresult(JSONObject propPayResult, String sREQSTS, String sRTNFLG) {

		ServiceResult serviceResult = new ServiceResult(BorrowOrder.PAY_ERROR, "未知异常，请稍后重试！");
		try {
			if (sREQSTS.equals("FIN") && sRTNFLG.equals("F")) {

				serviceResult = new ServiceResult(BorrowOrder.SUB_PAY_FAIL, "银行支付失败");
				logger.info(serviceResult.getCode() + ">>" + serviceResult.getMsg());
			} else if (sREQSTS.equals("FIN") && sRTNFLG.equals("S")) {
				serviceResult.setCode(BorrowOrder.SUB_PAY_SUCC);
				serviceResult.setMsg("银行支付成功");
				logger.info(serviceResult.getCode() + ">>" + serviceResult.getMsg());
			} else {
				serviceResult = new ServiceResult(BorrowOrder.PAY_SUBMIT, "支付已被银行受理（支付状态：" + sREQSTS + "）");
				logger.info(serviceResult.getCode() + ">>" + serviceResult.getMsg());
			}
		} catch (Exception e) {
			// TODO: handle exception
		}

		return serviceResult;

	}

	@Override
	public void payLoanInterestPays() {

		updatePayLoanInterestPaysB();
		updatePayLoanInterestPaysC();
	}

	/**
	 * 公司服务费打款
	 */
	public void updatePayLoanInterestPaysB() {
		try {

			// 打款B
			String cardNoB = "1001171609200156829";// 銀行卡号
			String cardNumberB = "102100099996";// 银行号
			String userPhoneB = "";// 手机号
			String realNameB = "上海斡创信息技术有限公司";// 姓名
			Integer isCmbB = 2;// 是否招行

			Integer intoMoneyB = 0;// 打款金额单位分
			List<Integer> listIdB = new ArrayList<Integer>();
			HashMap<String, Object> params = new HashMap<String, Object>();
			params.put("status", BorrowOrderLoan.DFK);
			params.put("payStatus", BorrowOrder.SUB_PAY_CSZT);
			// 查询出所有待打款的记录
			List<BorrowOrderLoan> bdls = borrowOrderLoanDao.findParams(params);

			if (bdls != null && bdls.size() > 0) {
				for (BorrowOrderLoan bdl : bdls) {
					intoMoneyB += bdl.getLoanInterests();
					listIdB.add(bdl.getId());
				}

				if (intoMoneyB > 0) {
					String orderNoB = GenerateNo.payRecordNo("B");
					List<BorrowOrder> bos = new ArrayList<BorrowOrder>();
					BorrowOrder bo = new BorrowOrder();
					bo.setYurref(orderNoB);
					bo.setUserPhone(userPhoneB);
					bo.setCardNo(cardNoB);
					bo.setRealname(realNameB);
					bo.setBankNumber(cardNumberB);
					bo.setIntoMoney(intoMoneyB);
					bos.add(bo);

					if (isCmbB == 2) {// 非招商
						// // 处理返回的结果
						ServiceResult serviceResult = borrowOrderService.subPayNotCmb(bos, OutOrders.act_NTQRYEBP_B);

						String paystatus = serviceResult.getCode();
						String payRemark = serviceResult.getMsg();
						// 更新借款表
						logger.info("result NTQRYEBP_B Loan :" + serviceResult.getCode() + ">>" + serviceResult.getMsg());

						HashMap<String, Object> paramsUpdate = new HashMap<String, Object>();
						Date fkDate = new Date();
						paramsUpdate.put("ids", listIdB);
						paramsUpdate.put("yurref", orderNoB);
						paramsUpdate.put("payRemark", payRemark);
						paramsUpdate.put("payStatus", paystatus);
						paramsUpdate.put("updatedAt", fkDate);
						if (paystatus.equals(BorrowOrder.SUB_SUBMIT)) {
							paramsUpdate.put("status", BorrowOrderLoan.DKZ);
							logger.info("updatePayLoanInterestPaysB  submit success  公司服务费打款提交成功！");
						}

						// if (paystatus.equals(BorrowOrder.SUB_PAY_SUCC)) {
						// paramsUpdate.put("status", BorrowOrderLoan.FKCG);
						// paramsUpdate.put("loanTime", fkDate);
						// }
						borrowOrderLoanDao.updateByParams(paramsUpdate);

					}

					else if (isCmbB == 1) {// 招行
						// ServiceResult serviceResult
						// =borrowOrderService.subPayNotCmb(bos,
						// OutOrders.act_NTQRYEBP_A);

					}

				}

			}
		} catch (Exception e) {
			logger.error("NTQRYEBP Loan B error");
			e.printStackTrace();
		}
	}

	/**
	 * 利全个人服务费打款
	 * 
	 */
	public void updatePayLoanInterestPaysC() {

		try {

			// 打款C
			String cardNoC = "6225882140980282";// 銀行卡号
			String cardNumberC = "308584000013";// 银行号
			String userPhoneC = "18817322995";// 手机号
			String realNameC = "林杰";// 姓名
			Integer isCmbC = 1;// 是否招行
			Integer intoMoneyC = 0;// 打款金额单位分

			//
			// Integer intoMoneyB = 0;// 打款金额单位分
			List<Integer> listIdC = new ArrayList<Integer>();
			HashMap<String, Object> params = new HashMap<String, Object>();
			params.put("status", BorrowOrderLoan.DFK);
			params.put("payStatus", BorrowOrder.SUB_PAY_CSZT);
			// 查询出所有待打款的记录
			List<BorrowOrderLoanPerson> bdlPersons = borrowOrderLoanPersonDao.findParams(params);

			if (bdlPersons != null && bdlPersons.size() > 0) {
				for (BorrowOrderLoanPerson bdl : bdlPersons) {
					intoMoneyC += bdl.getLoanInterests();
					listIdC.add(bdl.getId());
				}

				if (intoMoneyC > 0) {

					String orderNoC = GenerateNo.payRecordNo("C");
					List<BorrowOrder> bos = new ArrayList<BorrowOrder>();
					BorrowOrder bo = new BorrowOrder();
					bo.setYurref(orderNoC);
					bo.setUserPhone(userPhoneC);
					bo.setCardNo(cardNoC);
					bo.setRealname(realNameC);
					bo.setBankNumber(cardNumberC);
					bo.setIntoMoney(intoMoneyC);

					bos.add(bo);
					if (isCmbC == 1) {// 招行

						ServiceResult serviceResult = borrowOrderService.subPayCmb(bos, OutOrders.act_AgentRequest_C);
						String paystatus = serviceResult.getCode();
						String payRemark = serviceResult.getMsg();
						// 更新借款表
						logger.info("result  Loan  " + OutOrders.act_AgentRequest_C + ":" + serviceResult.getCode() + ">>" + serviceResult.getMsg());

						HashMap<String, Object> paramsUpdate = new HashMap<String, Object>();
						Date fkDate = new Date();
						paramsUpdate.put("ids", listIdC);
						paramsUpdate.put("yurref", orderNoC);
						paramsUpdate.put("payRemark", payRemark);
						paramsUpdate.put("payStatus", paystatus);
						paramsUpdate.put("updatedAt", fkDate);
						if (paystatus.equals(BorrowOrder.SUB_SUBMIT)) {
							paramsUpdate.put("status", BorrowOrderLoan.DKZ);
							logger.info("updatePayLoanInterestPaysC  submit success  个人服务费打款提交成功！");
						}
						// if (paystatus.equals(BorrowOrder.SUB_PAY_SUCC)) {
						// paramsUpdate.put("status", BorrowOrderLoan.FKCG);
						// paramsUpdate.put("loanTime", fkDate);
						// }
						borrowOrderLoanPersonDao.updateByParams(paramsUpdate);

					} else if (isCmbC == 2) {// 非招行

					}
				}

			}

		} catch (Exception e) {
			logger.error("updatePayLoanInterestPaysC Loan C error");
			e.printStackTrace();
		}
	}

	@Override
	public void updateLoanTerm() {
		// 非招行打款
		HashMap<String, Object> paramsM = new HashMap<String, Object>();
		paramsM.put("borrowStatus", BorrowOrder.STATUS_FKZ);
		paramsM.put("payStatus", BorrowOrder.SUB_PAY_CSZT);
		paramsM.put("queryType", "subPay");
		paramsM.put("querylimit", 10);
		try {

			try {
				paramsM.put("bankIscmb", 2);
				List<BorrowOrder> bos = borrowOrderService.findAll(paramsM);
				if (bos != null && bos.size() > 0) {
					logger.info("begin   Loan  ======" + OutOrders.act_NTQRYEBP_A);
					borrowOrderService.requestPaysNotCmbUser(bos);
					logger.info("end   Loan  ======" + OutOrders.act_NTQRYEBP_A);
				}
			} catch (Exception e) {
				logger.error("error   Loan  ======" + OutOrders.act_NTQRYEBP_A);
			}

			try {
				// 招行打款
				paramsM.put("bankIscmb", 1);
				paramsM.put("querylimit", 1);
				List<BorrowOrder> bocs = borrowOrderService.findAll(paramsM);
				if (bocs != null && bocs.size() > 0) {
					logger.info("begin   Loan  ======" + OutOrders.act_AgentRequest_A);
					borrowOrderService.requestPaysCmbUser(bocs);
					logger.info("end   Loan  ======" + OutOrders.act_AgentRequest_A);
				}
			} catch (Exception e) {
				logger.error("error   Loan  ======" + OutOrders.act_AgentRequest_A);
			}

			Thread.sleep(5000);

			queryPaysStateNotCmb();
			
			queryPaysStateCmb();

		} catch (Exception e) {
			logger.error("updateLoanTerm error",e);
		}

	}
	
	@Override
    public void channelReport() {
    	logger.info("推广统计 开始-------------->channelReport start");
    	
    	channelReportService.saveChannelReport();
    	
    	logger.info("推广统计 结束-------------->channelReport end");
    }
}
